package com.gouqi.tools.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gouqi.tools.common.ErrorCode;
import com.gouqi.tools.constant.CommonConstant;
import com.gouqi.tools.exception.BusinessException;
import com.gouqi.tools.mapper.UserToolMapper;
import com.gouqi.tools.model.dto.usertool.UserToolQueryRequest;
import com.gouqi.gouqitoolscommon.model.entity.UserTool;
import com.gouqi.tools.model.vo.UserToolVO;
import com.gouqi.tools.service.UserToolService;
import com.gouqi.tools.utils.SqlUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description 针对表【user_tool(用户调用工具关系)】的数据库操作Service实现
 */
@Service
public class UserToolServiceImpl extends ServiceImpl<UserToolMapper, UserTool> implements UserToolService {

    /**
     * 校验
     *
     * @param userTool
     * @param add
     */
    @Override
    public void validUserTool(UserTool userTool, boolean add) {
        //如果工具信息为空，为空抛出异常
        if (userTool == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 创建时，参数不能为空
        if (add) {
            if (userTool.getToolId().length() == 0 || userTool.getId().length() == 0) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口或用户不存在");
            }
        }
        if (userTool.getLeftNum() < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "剩余次数不能小于0");
        }
    }

    /**
     * 获取查询条件
     *
     * @param userToolQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<UserTool> getQueryWrapper(UserToolQueryRequest userToolQueryRequest) {
        QueryWrapper<UserTool> queryWrapper = new QueryWrapper<>();
        if (userToolQueryRequest == null) {
            return queryWrapper;
        }
        String id = userToolQueryRequest.getId();
        String userId = userToolQueryRequest.getUserId();
        String toolId = userToolQueryRequest.getToolId();
        Integer totalNum = userToolQueryRequest.getTotalNum();
        Integer leftNum = userToolQueryRequest.getLeftNum();
        Integer status = userToolQueryRequest.getStatus();
        String sortField = userToolQueryRequest.getSortField();
        String sortOrder = userToolQueryRequest.getSortOrder();

        // 拼接查询条件

        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        return queryWrapper;
    }

    /**
     * 获取用户调用工具关系封装
     *
     * @param userTool
     * @param request
     * @return
     */
    @Override
    public UserToolVO getUserToolVO(UserTool userTool, HttpServletRequest request) {

        return null;
    }

    /**
     * 分页获取用户调用工具关系封装
     *
     * @param userToolPage
     * @param request
     * @return
     */
    @Override
    public Page<UserToolVO> getUserToolVOPage(Page<UserTool> userToolPage, HttpServletRequest request) {
        List<UserTool> userToolList = userToolPage.getRecords();
        Page<UserToolVO> userToolVOPage = new Page<>(userToolPage.getCurrent(), userToolPage.getSize(), userToolPage.getTotal());
        if (CollectionUtils.isEmpty(userToolList)) {
            return userToolVOPage;
        }
        return userToolVOPage;
    }

    @Override
    public boolean invokeCount(String userToolId, String userId) {
        if (userToolId.length() == 0 || userId.length() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UpdateWrapper<UserTool> userToolUpdateWrapper = new UpdateWrapper<>();
        userToolUpdateWrapper.eq("userToolId", userToolId);
        userToolUpdateWrapper.eq("userId", userId);
        userToolUpdateWrapper.setSql("leftNum = leftNum - 1, totalNum = totalNum + 1");
        return this.update(userToolUpdateWrapper);
    }

}




